package com.base.mail.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.base.commonlib.utils.RxBus2;
import com.base.mail.R;
import com.base.mail.databinding.ItemTreePartenBinding;
import com.base.mail.tree.Node;
import com.base.mail.tree.TreeRecyclerAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangke on 2017-1-14.
 */
public class AJParterAdapter extends TreeRecyclerAdapter {

    public static Map<String,Node> mMapPart = new HashMap<>();
    private int type_dept= 1;
    private int type_user= 6;

    private List<Node> mData;

    public AJParterAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
        this.mData = datas;
    }

    public AJParterAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel);
        this.mData = datas;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHoder(View.inflate(mContext, R.layout.item_tree_parten,null));
    }

    @Override
    public void onBindViewHolder(final Node node, final RecyclerView.ViewHolder holder, int position) {
//       if (holder instanceof MyHoder){
           ((MyHoder) holder).mBinding.setEntity(node);
           if (node.getState().equals(type_user+"")){
               ((MyHoder) holder).mBinding.depart.setVisibility(View.GONE);
               ((MyHoder) holder).mBinding.user.setVisibility(View.VISIBLE);
               final CheckBox box = new CheckBox(mContext);
               box.setChecked(node.isChecked());
//               if (node.isChecked()){
//                   mMapPart.put(node.getId().toString(),node);
//               }else {
//                   if (mMapPart.containsKey(node.getId().toString())){
//                       mMapPart.remove(node.getId().toString());
//                   }
//               }
               ((MyHoder) holder).mBinding.root.removeAllViews();
               ((MyHoder) holder).mBinding.root.addView(box);
               box.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       RxBus2.getInstance().post(new Node());
                       if (box.isChecked()){
                           mMapPart.put(node.getId().toString(),node);
                           node.setChecked(true);
                       }else {
                           node.setChecked(false);
                           if (mMapPart.containsKey(node.getId().toString())){
                               mMapPart.remove(node.getId().toString());
                           }
                       }
                   }
               });
           }else {
               ((MyHoder) holder).mBinding.depart.setVisibility(View.VISIBLE);
               ((MyHoder) holder).mBinding.user.setVisibility(View.GONE);
           }

//       }else if (holder instanceof UserHolder){
//           ((UserHolder) holder).mBinding.setEntity(node);
//       }
    }

    class MyHoder extends RecyclerView.ViewHolder {

        ItemTreePartenBinding mBinding;
        public MyHoder(View itemView) {
            super(itemView);
            mBinding = DataBindingUtil.bind(itemView);
        }

    }

}
